package net.xinqushi.gc;

import java.util.ArrayList;
/**
 *  数组中的引用类型，为空时会被回收
 *  ArrayList remove之后不会立刻回收，因为是元素前移动操作。所以
 *  被remove掉的Reference并没有置为NULL，只是被数组遗弃了，其他元素覆盖了索引位置。
 */
public class ArrayGC {

    public static void main(String[] args) {
        Reference[] references = new Reference[10];
        for (int i = 0 ; i < 10 ;i++) {
            references[i] = new Reference();
        }
        references[1] = null;
        //System.gc();
        ArrayList<Reference> list = new ArrayList<>(10);
        Reference reference = null;
        for (int i = 0 ; i < 10 ;i++) {
            list.add(reference = new Reference());
        }
        list.remove(1);
        //reference = null;
        System.gc();
    }

    static class Reference{
        String name;

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
            System.out.println(this.getClass() + " finalized ...");
        }
    }
}
